package com.cdn.agent;

import com.cdn.agent.enums.OrmType;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.plugin.Invocation;

import java.util.HashMap;
import java.util.Map;


/**
 * 蔡定努
 * 2024/08/27 13:28
 */
public abstract class IPrintLog {

    public static Map<OrmType, IPrintLog> BEAN = new HashMap<>();

    protected static final Log SQL_LOG = LogFactory.getLog("org.apache.ibatis.jdbc");

    protected abstract void printLog(Invocation invocation, long time);

    protected static void logs(long time, String sql, String sqlId) {
        StringBuilder sb = new StringBuilder()
                .append("--------------------------\n")
                .append("Time：").append(time)
                .append(" ms \nTarget method：").append(sqlId)
                .append("\n").append("Execute SQL：\n\n\t")
                .append(sql).append(";").append("\n")
                .append("--------------------------\n");
        SQL_LOG.info("\n\n\n " + sb.toString());
    }

    protected void regist(IPrintLog log, OrmType ormtYpe) {
        BEAN.put(ormtYpe, log);
    }


    public static void print(Invocation invocation, long time, OrmType ormtYpe){
        BEAN.get(ormtYpe).printLog(invocation, time);
    }

}
